package com.emqx;

import com.alibaba.fastjson.JSONObject;
import com.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @Author: chihaojie
 * @Date: 2019/5/30 17:57
 * @Version 1.0
 * @Note
 */

@Component
public class MqttMessageSender {

    @Autowired
    private MqttGateway mqttGateway;


    public void   send(MqttMessageDTO  msg){
        if(!ObjectUtils.isEmpty(msg)){
            String data=JSONObject.toJSONString(msg);
            String topic=msg.getMsgReceiver();
            mqttGateway.sendToMqtt(topic,data);
        }
    }


    public  void  send(String  destTopic,String msg,String msgType){
        Date now  =new  Date();
        MqttMessageDTO  msgDto=new MqttMessageDTO();
        msgDto.setSendTime(now);
        msgDto.setMsgType(msgType);
        msgDto.setMsgIdentifier(DateUtil.getCurrTime());
        msgDto.setMsgBody(msg);
        msgDto.setSendStatus("ALREADY_SENDED");
        msgDto.setMsgReceiver(destTopic);
        String  sendData=JSONObject.toJSONString(msg);
        mqttGateway.sendToMqtt(destTopic,sendData);
    }

}
